package com.zhudeshuai.zhudemolist.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.zhudeshuai.zhudemolist.R;

/**
 * @author zhudeshuai
 * @since 17/12/7
 */

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {

	private Context mContext;

	public ImageAdapter(Context context) {
		mContext = context;
	}

	@Override
	public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

		ImageView item = new ImageView(mContext);
		item.setScaleType(ImageView.ScaleType.CENTER_CROP);
		RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
		params.setMargins(10,10,10,10);
		item.setLayoutParams(params);
		return new ImageViewHolder(item);
	}

	@Override
	public void onBindViewHolder(ImageViewHolder holder, int position) {
		final ImageView itemView = (ImageView) holder.itemView;
		itemView.setImageResource(R.drawable.ic_launcher);
		itemView.post(new Runnable() {
			@Override
			public void run() {
				int height = itemView.getHeight();
				ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
				layoutParams.width = height;
				itemView.setLayoutParams(layoutParams);
			}
		});
	}

	@Override
	public int getItemCount() {
		return 9;
	}

	class ImageViewHolder extends RecyclerView.ViewHolder {

		public ImageViewHolder(View itemView) {
			super(itemView);
		}
	}
}



